package casa.depesos.app.com.activity;

import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.common.base.base.AbsBaseActivity;
import com.common.base.utils.WeakHandler;

import java.util.List;

import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.Keep1Dlg;
import casa.depesos.app.com.dlg.Keep2Dlg;
import casa.depesos.app.com.dlg.LoadingDlg;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public abstract class BaseActivity extends AbsBaseActivity {

    protected WeakHandler h = new WeakHandler();
    LoadingDlg dlg;

    @Override
    protected boolean isBlack() {
        return false;
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        if (needPadding()) {
            view.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        }
    }

    protected boolean needPadding() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        h.removeCallbacksAndMessages(null);
    }

    public void showLoading() {
        showLoading(true);
    }

    public void showLoading(boolean cancelable) {
        try {
            if (dlg == null) {
                dlg = new LoadingDlg();
                dlg.setCancelable(cancelable);
                dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                    @Override
                    public void onDismiss(boolean isOk, Object value) {
                        dlg = null;
                    }
                });
//            dlg.setLoadingRenderer(new LevelLoadingRenderer.Builder(this).setWidth(140).setHeight(140).setCenterRadius(45).setStrokeWidth(10).build());
            }
            if (dlg.isAdded()) {
                return;
            }
            dlg.show(getSupportFragmentManager(), "loading");
        } finally {
            if (dlg != null) {
                dlg.setCancelable(cancelable);
            }
        }
    }

    public void hideLoading() {
        if (dlg != null) {
            dlg.dismiss();
        }
    }

    public boolean isLoading() {
        if (dlg == null) {
            return false;
        }
        return dlg.isAdded();
    }

    protected void dealBack() {
        if (this instanceof CardActivity) {
            Report.click("ACTION_LL_002");
        } else if (this instanceof FaceActivity) {
            Report.click("ACTION_MM_002");
        } else if (this instanceof BankActivity) {
            Report.click("ACTION_NN_002");
        }
        Report.show("ACTION_OO_001");
        new Keep1Dlg().setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (!isOk) {
                    Report.click("ACTION_OO_003");
                    Report.show("ACTION_OO_004");
                    new Keep2Dlg().setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                        @Override
                        public void onDismiss(boolean isOk, Object value) {
                            if (!isOk) {
                                Report.click("ACTION_OO_006");
                                finish();
                            } else {
                                Report.click("ACTION_OO_005");
                            }
                        }
                    }).show(getSupportFragmentManager(), "");
                } else {
                    Report.click("ACTION_OO_002");
                }
            }
        }).show(getSupportFragmentManager(), "");
    }

    protected boolean needKeep() {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (needKeep()) {
                dealBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public void clearFragment(FragmentManager manager) {
        try {
            List<Fragment> list = manager.getFragments();
            for (Fragment f : list) {
                removeFragment(manager, f);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
